<template>
  <div class="userLoginView">
    <a-form
      style="
        max-width: 450px;
        margin: 0 auto;
        border: 1px solid #f1ecec;
        padding: 30px 40px 50px 40px;
      "
      label-align="left"
      auto-label-width
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item>
        <img style="max-width: 90px" src="../../../public/logo.png" alt="" />
        <h2 style="margin: 0 auto; color: #6d9fc7">智能BI平台-用户登录</h2>
      </a-form-item>
      <a-form-item name="userName" label="账号">
        <a-input v-model:value="form.userName" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item name="password" tooltip="密码不少于8位" label="密码">
        <a-input-password
          v-model:value="form.password"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button
          type="primary"
          html-type="submit"
          style="width: 110px; margin: 0 auto"
          >登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { LoginUserDto, UserControllerService } from "../../../generated";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { message } from "ant-design-vue";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { setToken } from "@/utils/auth";

const router = useRouter();
const store = useStore();
/**
 * 表单信息
 */
const form = reactive({
  userName: "lzh",
  password: "1234",
}) as LoginUserDto;
/**
 * 提交表单
 */
const handleSubmit = async () => {
  const res = await UserControllerService.loginUsingPost(form);
  // 登录成功，跳转到主页
  if (res.code === 200) {
    console.log(res.data);
    setToken(res.data.token);
    await store.dispatch("user/getLoginUser");
    await router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败：" + res.message);
  }
};
</script>

<style scoped>
.userLoginView {
  display: flex;
  align-items: center; /* 垂直居中 */
  height: 400px;
}

#particles-js {
  width: 100%;
  height: calc(100% - 100px);
  position: absolute;
  z-index: -1;
}
</style>
